package com.ludomancy.sora;

public class Edge
{
	public Vertex v1;
	public Vertex v2;
	public double restLength;
	
	public Edge(Vertex v1, Vertex v2)
	{
		this.v1 = v1;
		this.v2 = v2;
		restLength = length();
	}
		
	public void applyForceAtPoint(double t, double x, double y)
	{
		v2.forceX += x*t;
		v2.forceY += y*t;
		v1.forceX += x*(1-t);
		v1.forceY += y*(1-t);
	}
	
	public void moveAtPoint(double t, double x, double y)
	{
		double lambda = (1-t)*(1-t) + t*t;
		v1.px += (1-t)*x/lambda;
		v1.py += (1-t)*y/lambda;
		
		v2.px += t*x/lambda;
		v2.py += t*y/lambda;

		v1.old_px += (1-t)*x/lambda;
		v1.old_py += (1-t)*y/lambda;
		
		v2.old_px += t*x/lambda;
		v2.old_py += t*y/lambda;
	}
	
	public void pushAtPoint(double t, double x, double y)
	{
		double lambda = (1-t)*(1-t) + t*t;
		v1.px += (1-t)*x/lambda;
		v1.py += (1-t)*y/lambda;
		
		v2.px += t*x/lambda;
		v2.py += t*y/lambda;
	}
	
	public double[] getPositionAtPoint(double t)
	{
		double[] ret = new double[2];
		ret[0] = v1.px*(1-t) + v2.px*t;
		ret[1] = v1.py*(1-t) + v2.py*t;
		return ret;
	}
	
	public void setVelocityAtPoint(double t, double x, double y)
	{
		v1.old_px = v1.px - x*(1-t) - v1.velX()*t;
		v1.old_py = v1.py - y*(1-t) - v1.velY()*t;
		
		v2.old_px = v2.px - x*t - v2.velX()*(1-t);
		v2.old_py = v2.py - y*t - v2.velY()*(1-t);
	}
		
	public double[] getVelocityAtPoint(double t)
	{
		double[] ret = new double[2];
		ret[0] = v1.velX()*(1-t) + v2.velX()*t;
		ret[1] = v1.velY()*(1-t) + v2.velY()*t;
		return ret;
	}

	public double[] getForceAtPoint(double t)
	{
		double[] ret = new double[2];
		ret[0] = v2.forceX*t + v1.forceX*(1-t);
		ret[1] = v2.forceY*t + v1.forceY*(1-t);
		return ret;
	}
	
	public double effort()
	{
		return 1 - length()/restLength;
	}
	
	public double length()
	{
		double ox = v1.px - v2.px;
		double oy = v1.py - v2.py;
		return Math.sqrt(ox*ox + oy*oy);
	}
}
